package action.student;

import beans.Assignment;
import dao.MngAssignment;
import dao.Student;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class addStudentInClass extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String acc_id = request.getParameter("acc_id");
        String class_id = request.getParameter("class_id");
        Student st = new Student();
        MngAssignment mngAssign = new MngAssignment();
        List<Assignment> listAssign = mngAssign.listAssignByClass(class_id);
        String msg = "";
        String error = "";
        if (acc_id != null && !acc_id.equals("")) {
            if (st.addStudentinClass(acc_id, class_id)) {
                for (Assignment assign : listAssign) {
                    assign.setAcc_id(acc_id);
                    mngAssign.addAssignForNewStudent(assign);
                }
                msg = "Add Student successful !";
                request.setAttribute("msg", msg);
                request.setAttribute("class_id", class_id);
                request.setAttribute("command", "addStudent");
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=account");
                rd.forward(request, response);
            } else {
                error = "Add Student Fail !";
                request.setAttribute("error", error);
                request.setAttribute("class_id", class_id);
                request.setAttribute("command", "addStudent");
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=addStudentInClass");
                rd.forward(request, response);
            }
        } else {
            error = "Add Student error - Insert Student!";
            request.setAttribute("error", error);
            request.setAttribute("class_id", class_id);
            request.setAttribute("command", "addStudent");
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=addStudentInClass");
            rd.forward(request, response);
        }
    }
}
